var util = require('../../../util/common');

module.exports = function(app) {
    return new Handler(app);
};

var Handler = function(app) {
    this.app = app;
};

var handler = Handler.prototype;

handler.queryEntry = function(msg, session, next) {
    var connectors = this.app.getServersByType('connector');
    if (!connectors || connectors.length === 0) {
        next(null, {
            code: 500
        });
        return;
    }
    var id = util.getRandomNum(0, connectors.length - 1);
    next(null, {
        code: 200,
        host: connectors[id].host,
        port: connectors[id].clientPort,
        sid: connectors[id].id
    });

    // 此处网关服务器应该主动断开同客户端的连接，不应该等待客户端主动断开连接
    var sessionService = this.app.get('sessionService');
    sessionService.kickBySessionId(session.id);
};